package eu.connect.im;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.ProtocolVersion;
import org.apache.http.impl.DefaultHttpResponseFactory;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.DefaultedHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.SyncBasicHttpParams;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;


public class Utils
{
	public static String decodeHttpEntity(HttpEntity entity)
	{
		StringBuffer sb = new StringBuffer();
		try
		{
			InputStream in = entity.getContent();
			byte bytes[] = new byte[1024];
			int length;
			while ((length = in.read(bytes))>0)
			{
				String s = new String(bytes);
				s.substring(0, length);
				sb.append(s);
			}
			
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sb.toString();
	}
	
	public static String getOMSFieldValue(String entity, String field)
	{
		String s1 = "</", s2 = "<", s3 = ">";
		String s4 = s1 + field + s3, s5 = s2 +field + s3;
		int pos;
		String result;
		
		pos = entity.indexOf(s4);
		result = entity.substring(0, pos);
		pos = entity.indexOf(s5);
		result = result.substring(pos);
		result = result.substring(field.length()+2);
		
		return result;
		
	}
	
	public static String modifyOMSFieldValue(String entity, String field, String newValue)
	{
		String s1 = "</", s2 = "<", s3 = ">";
		String s4 = s1 + field + s3, s5 = s2 +field + s3;
		int pos;
		
		pos = entity.indexOf(s4);
		String temp1 = entity.substring(pos);
		pos = entity.indexOf(s5);
		String temp2 = entity.substring(0, pos+2+field.length());
		
		return temp2 + newValue + temp1;
		
	}
	
	public static HttpResponse getAResponceMessage(ProtocolVersion ver)
	{
		HttpResponse response;
		HttpContext context = new BasicHttpContext(null);
		response = new DefaultHttpResponseFactory().newHttpResponse(ver, 200, context);
		HttpParams params = new SyncBasicHttpParams();
        params
        	.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 5000)
        	.setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
        	.setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false)
        	.setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
        	.setParameter(CoreProtocolPNames.ORIGIN_SERVER, "HttpComponents/1.1");
		response.setParams(new DefaultedHttpParams(response.getParams(), params));
		response.setStatusCode(HttpStatus.SC_OK);
		
		return response;
		
	}
	
	public static String getDateTime()
	{
		Date nowTime = new Date();
		SimpleDateFormat matter = new SimpleDateFormat("MM-dd HH:mm");
		return matter.format(nowTime);

	}
	
}
